package com.example.multinetty.netty.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.DispatcherServlet;

@Component
//未来要增加一个双锁定的单列模式
public class DispatchServletFactory implements ApplicationContextAware{
	
	private ApplicationContext applicationContext;

	static Map<String, MyDispatcherServlet> map = new HashMap<>();
	
    @Override
    public void setApplicationContext(ApplicationContext applicationContext){
        this.applicationContext = applicationContext;
    }

	public DispatcherServlet getByProxy(String name) {
		MyDispatcherServlet servlet = map.get(name);
		if (servlet == null) {
			servlet = applicationContext.getBean(MyDispatcherServlet.class);
			servlet.initContext(applicationContext);
			map.put(name, servlet);
		}
		
		return servlet;
	}
	
}
